param location string = resourceGroup().location
param prefix string

@description('Virtual network resource name.')
param virtualNetworkName string

@description('Virtual network resource Address Space.')
param virtualNetworkAddressSpace array

@description('Virtual network resource Subnet name.')
param subnetName1 string
param subnetName2 string

@description('Virtual network resource Subnet Address Prefix.')
param subnetAddressPrefix1 string
param subnetAddressPrefix2 string

//@description('The base URI where artifacts required by this template are located')
//param _artifactsLocation string = deployment().properties.templateLink.uri
//var installScriptUri = uri(_artifactsLocation, 'scripts/helm.sh${_artifactsLocationSasToken}')
//param _artifactsLocationSasToken string = ''

param clusterVmSize string

@description('Public Helm Repo Name')
param helmRepo string

@description('Public Helm Repo URL')
param helmRepoURL string

@description('Public Helm App')
param helmApp string

@description('Public Helm App Name')
param helmAppName string

param grafanaObject object

module aksManagedIdentity 'modules/identity/aksidentity.bicep' = {
  name: '${prefix}-aks'
  params: {
    location: location
    prefix: prefix
  }
}

module network 'modules/network/network.bicep' = {
  name: 'Network'
  params: {
    location: location
    subnetAddressPrefix1: subnetAddressPrefix1
    subnetAddressPrefix2: subnetAddressPrefix2
    subnetName1: subnetName1
    subnetName2: subnetName2
    virtualNetworkAddressSpace: virtualNetworkAddressSpace
    virtualNetworkName: virtualNetworkName
  }
}

module kubernetes 'modules/kubernetes/kubernetes.bicep' = {
  name: 'Kubernetes'
  params: {
    location: location
    prefix: prefix
    subnetId: network.outputs.subnet2Id
    userAssignedIdentities: aksManagedIdentity.outputs.aksManagedIdentityId
    vmSize: clusterVmSize
    helmRepo: helmRepo
    helmRepoURL: helmRepoURL
    helmApp: helmApp
    helmAppName: helmAppName
  }
}

module grafana 'modules/grafana/grafana.bicep' = {
  name: 'Grafana'
  params: {
    location: location
    prefix: prefix
    apiKey: grafanaObject.apiKey
    autoGeneratedDomainNameLabelScope: grafanaObject.autoGeneratedDomainNameLabelScope
    deterministicOutboundIP: grafanaObject.deterministicOutboundIP
    grafanaMajorVersion: grafanaObject.grafanaMajorVersion
    grafanaSkuName: grafanaObject.grafanaSkuName
    publicNetworkAccess: grafanaObject.publicNetworkAccess
    smtp: grafanaObject.grafanaConfigurations.smtp
    zoneRedundancy: grafanaObject.zoneRedundancy
    privateDnsZoneName: grafanaObject.privateDnsZoneName
    privateLinkServiceUrl: grafanaObject.privateLinkServiceUrl
    virtualNetworkName: virtualNetworkName
    virtualNetworkId: network.outputs.virtualNetworkId
    subnetId: network.outputs.subnet1Id
    helmOutput: kubernetes.outputs.helmOutput
  }
}

output grafanaEndpoint string = grafana.outputs.grafaneEndpoint
output mpeplsEndpoint string = grafana.outputs.mpeplsEndpoint
